<?php
/**
 * Created by PhpStorm.
 * User: tangchao
 * Date: 19-3-18
 * Time: 上午11:42
 */

namespace App\Lib\AliyunSdk;

require_once EASYSWOOLE_ROOT.'/App/Lib/AliyunSdk/aliyun-php-sdk-core/Config.php';   // 假定您的源码文件和aliyun-php-sdk处于同一目录。
require_once EASYSWOOLE_ROOT.'/App/Lib/AliyunSdk/aliyun-oss-php-sdk-master/autoload.php';
use vod\Request\V20170321 as vod;
use OSS\OssClient;
use OSS\Core\OssException;
use Sts\Request\V20150401 as Sts;


class AliVod
{
    public $regionId = 'cn-shanghai';
    public $endPoint = 'sts.cn-shanghai.aliyuncs.com';
    public $accessKeyId;
    public $accessKeySecret;
    public $client;
    public $ossClient;

    /**
     * 1.初始化vod客户端
     * AliVod constructor.
     */
    public function __construct(){

        $this->accessKeyId = \Yaconf::get('easyswoole.aliyun.accessKeyId');
        $this->accessKeySecret = \Yaconf::get('easyswoole.aliyun.accessKeySecret');
        \DefaultProfile::addEndpoint($this->regionId, $this->regionId, "Sts", $this->endPoint);
        $profile = \DefaultProfile::getProfile($this->regionId, $this->accessKeyId, $this->accessKeySecret);
        $this->client = new \DefaultAcsClient($profile);
    }

    /**
     * 2.获取视频上传地址和凭证
     * @param $title
     * @param $fileName
     * @param array $other
     * @return mixed|\SimpleXMLElement
     * @throws \ClientException
     * @throws \ServerException
     */
    public function createUploadVideo($title,$fileName,$other = []) {
        $request = new vod\CreateUploadVideoRequest();
        $request->setTitle($title);        // 视频标题(必填参数)
        $request->setFileName($fileName); // 视频源文件名称，必须包含扩展名(必填参数)
        if (!empty($other['desc'])){
            $request->setDescription($other['desc']);  // 视频源文件描述(可选)
        }
        if (!empty($other['cover'])){
            $request->setCoverURL($other['cover']);  // 自定义视频封面(可选)
        }
        if (!empty($other['tags'])){
            $request->setTags($other['tags']);  // 视频标签(可选)
        }
        $request->setAcceptFormat('JSON');

        $authRes = $this->client->getAcsResponse($request);
        if (empty($authRes) || empty($authRes->VideoId)){
            return $this->writeJson(500,'','获取上传凭证失败');
        }
        return $authRes;
    }

    /**
     * 刷新上传凭证
     * @param $videoId
     * @return mixed|\SimpleXMLElement
     * @throws \ClientException
     * @throws \ServerException
     */
    function refreshUploadVideo($videoId) {
        $request = new vod\RefreshUploadVideoRequest();
        $request->setVideoId($videoId);
        return $this->client->getAcsResponse($request);
    }

    /**
     * 初始化OSS客户端
     * @param $uploadAuth
     * @param $uploadAddress
     * @return OssClient
     * @throws OssException
     */
    public function initOssClient($uploadAuth, $uploadAddress) {
        $this->ossClient = new OssClient($uploadAuth['AccessKeyId'], $uploadAuth['AccessKeySecret'], $uploadAddress['Endpoint'],
            false, $uploadAuth['SecurityToken']);
        $this->ossClient->setTimeout(86400*7);    // 设置请求超时时间，单位秒，默认是5184000秒, 建议不要设置太小，如果上传文件很大，消耗的时间会比较长
        $this->ossClient->setConnectTimeout(10);  // 设置连接超时时间，单位秒，默认是10秒
        return $this->ossClient;
    }

    /**
     * 上传文件
     * @param $uploadAddress
     * @param $localFile
     * @return mixed
     */
    public function uploadLocalFile($uploadAddress, $localFile) {
        return $this->ossClient->uploadFile($uploadAddress['Bucket'], $uploadAddress['FileName'], $localFile);
    }

    /**
     * 获取播放信息
     * @param $videoId
     * @return mixed|\SimpleXMLElement
     * @throws \ClientException
     * @throws \ServerException
     */
    public function getPlayInfo($videoId) {
        $request = new vod\GetPlayInfoRequest();
        $request->setVideoId($videoId);
        $request->setAcceptFormat('JSON');
        return $this->client->getAcsResponse($request);
    }

    public function getPlayAuth($videoId) {
        $request = new vod\GetVideoPlayAuthRequest();
        $request->setVideoId($videoId);
        $request->setAuthInfoTimeout(3000);
        $response = $this->client->getAcsResponse($request);
        return $response;
    }

    public function getStsToken()
    {
        $roleArn = "acs:ram::1600098725355439:role/video";
        $policy=<<<POLICY
{
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "vod:*"
            ],
            "Resource": "*"
        }
    ],
    "Version": "1"
}
POLICY;
        $request = new Sts\AssumeRoleRequest();
        $request->setRoleSessionName("266706253065359304");
        $request->setRoleArn($roleArn);
        $request->setPolicy($policy);
        $request->setDurationSeconds(3600);
        $response = $this->client->getAcsResponse($request);
        return $response;
    }


}